<script setup lang="ts">
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { MeetingNotifyTaskVO } from '#/api/meeting/notify/model';

import { computed, ref } from 'vue';

import { useVbenDrawer, useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { getVxePopupContainer } from '@vben/utils';

import { Modal, Popconfirm, Space } from 'ant-design-vue';

import { useVbenVxeGrid, vxeCheckboxChecked } from '#/adapter/vxe-table';
import {
  meetingNotifyTaskPage,
  meetingNotifyTaskRemove,
} from '#/api/meeting/notify';

import meetingNotifyModal from './meeting-notify-modal.vue';
import { columns } from './notify';

// const emit = defineEmits<{ reload: [] }>();
const currMeetingId = ref<any>(null);
const currName = ref<any>('');
const title = computed(() => {
  return `${currName.value} - 消息通知列表`;
});

const [BasicDrawer, drawerApi] = useVbenDrawer({
  class: 'w-[80%]',
  onClosed: handleClosed,
  // onConfirm: handleConfirm,
  onOpenChange: async (isOpen) => {
    if (!isOpen) {
      return null;
    }
    drawerApi.lock(true);
    //
    const { id, name } = drawerApi.getData() as {
      id?: number | string;
      name?: string;
    };
    currMeetingId.value = id;
    currName.value = name;

    drawerApi.lock(false);
  },
});

async function handleClosed() {
  currMeetingId.value = null;
  currName.value = '';
}

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    // trigger: 'row',
  },
  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        return await meetingNotifyTaskPage({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
          meetingId: currMeetingId.value,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  id: 'meeting-notify-list-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  gridOptions,
});

// 新增编辑
const [MeetingNotifyModal, modalApi] = useVbenModal({
  connectedComponent: meetingNotifyModal,
});

function handleAdd() {
  modalApi.setData({ meetingId: currMeetingId.value });
  modalApi.open();
}

async function handleEdit(row: MeetingNotifyTaskVO) {
  modalApi.setData({ id: row.id, meetingId: currMeetingId.value });
  modalApi.open();
}

async function handleDelete(row: MeetingNotifyTaskVO) {
  await meetingNotifyTaskRemove(row.id);
  await tableApi.query();
}

function handleMultiDelete() {
  const rows = tableApi.grid.getCheckboxRecords();
  const ids = rows.map((row: MeetingNotifyTaskVO) => row.id);
  Modal.confirm({
    title: '提示',
    okType: 'danger',
    content: `确认删除选中的${ids.length}条记录吗？`,
    onOk: async () => {
      await meetingNotifyTaskRemove(ids);
      await tableApi.query();
    },
  });
}
</script>

<template>
  <BasicDrawer :footer="false" title="消息通知管理">
    <BasicTable :table-title="title">
      <template #toolbar-tools>
        <Space>
          <a-button
            :disabled="!vxeCheckboxChecked(tableApi)"
            danger
            type="primary"
            v-access:code="['meeting:notifyTask:remove']"
            @click="handleMultiDelete"
          >
            {{ $t('pages.common.delete') }}
          </a-button>
          <a-button
            type="primary"
            v-access:code="['meeting:notifyTask:add']"
            @click="handleAdd"
          >
            {{ $t('pages.common.add') }}
          </a-button>
        </Space>
      </template>
      <template #sendTime="{ row }">
        <span v-if="row.row">{{ row.sendTime }}</span>
        <span v-else>立即发送</span>
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button
            v-access:code="['meeting:notifyTask:edit']"
            @click.stop="handleEdit(row)"
          >
            {{ $t('pages.common.edit') }}
          </ghost-button>
          <Popconfirm
            :get-popup-container="getVxePopupContainer"
            placement="left"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <ghost-button
              danger
              v-access:code="['meeting:notifyTask:remove']"
              @click.stop=""
            >
              {{ $t('pages.common.delete') }}
            </ghost-button>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <MeetingNotifyModal @reload="tableApi.query()" />
  </BasicDrawer>
</template>
